#include "hv5523.h"
#include "max668.h"
#include "rtc.h"
#include "input.h"

//HV_CLK:IO27 HV_DIN:IO25 HV_LE:IO26
//N1右管，N2左管
//N1:5 3 6 4 0 1 8 2 9 7 LD RD X
//N2:LD RD 5 3 6 4 0 1 8 2 9 7

unsigned long HV5523_Buf;

void HV5523_Init(void)
{
    pinMode(13,OUTPUT);
    pinMode(15,OUTPUT);
    pinMode(4,OUTPUT);
    HV_CLK_L;
    HV_DIN_L;
    HV_LE_L;
    HV5523_Send_Data(0x0);//POL高电平，发送0表示漏极开路输出
}

void HV5523_Send_Data(unsigned long val)
{
    unsigned char i;
    unsigned long tmp = val;
    for(i = 0; i < 32; i++)
    {
        if(tmp & 1)
        HV_DIN_H;
        else HV_DIN_L;
        
        tmp >>= 1;
        
        HV_CLK_H;
        HV_CLK_L;
    }
    HV_LE_H;
    HV_LE_L;
    HV_DIN_L;
}

void Display_Num(unsigned char num,bool N2_LD,bool N2_RD,bool N1_LD,bool N1_RD)
{
    HV5523_Buf = 0x0;
    unsigned char n1 = num % 10;
    unsigned char n2 = num / 10 % 10;
    switch(n1)
    {
        case 0:HV5523_Buf ^= 1 << 8;break;
        case 1:HV5523_Buf ^= 1 << 9;break;
        case 2:HV5523_Buf ^= 1 << 11;break;
        case 3:HV5523_Buf ^= 1 << 5;break;
        case 4:HV5523_Buf ^= 1 << 7;break;
        case 5:HV5523_Buf ^= 1 << 4;break;
        case 6:HV5523_Buf ^= 1 << 6;break;
        case 7:HV5523_Buf ^= 1 << 13;break;
        case 8:HV5523_Buf ^= 1 << 10;break;
        case 9:HV5523_Buf ^= 1 << 12;break;
    }
    switch(n2)
    {
        case 0:HV5523_Buf ^= 1 << 23;break;
        case 1:HV5523_Buf ^= 1 << 24;break;
        case 2:HV5523_Buf ^= 1 << 26;break;
        case 3:HV5523_Buf ^= 1 << 20;break;
        case 4:HV5523_Buf ^= 1 << 22;break;
        case 5:HV5523_Buf ^= 1 << 19;break;
        case 6:HV5523_Buf ^= 1 << 21;break;
        case 7:HV5523_Buf ^= 1 << 28;break;
        case 8:HV5523_Buf ^= 1 << 25;break;
        case 9:HV5523_Buf ^= 1 << 27;break;
    }
    if(N2_LD)
        HV5523_Buf ^= 1 << 17;
    if(N2_RD)
        HV5523_Buf ^= 1 << 18;
    if(N1_LD)
        HV5523_Buf ^= 1 << 14;
    if(N1_RD)
        HV5523_Buf ^= 1 << 15;
    
    HV5523_Send_Data(HV5523_Buf);
}

void Display_Time(void)
{
    Display_Num(rtc.getHour(),1,1,0,0);
    delay(700);
    Display_Num(rtc.getMinute(),0,0,1,1);
    delay(700);
    Display_Num(rtc.getHour(),1,1,0,0);
    delay(700);
    Display_Num(rtc.getMinute(),0,0,1,1);
    delay(700);
    Display_Num(rtc.getSecond(),1,0,0,0);
    delay(1000);
    Display_Num(rtc.getSecond(),0,1,0,0);
    delay(1000);
    Display_Num(rtc.getSecond(),0,0,1,0);
    delay(1000);
    Display_Num(rtc.getSecond(),0,0,0,1);
    delay(1000);
}

void Display_Voltage(void)
{
    unsigned int VB_ADC = ReadVB;
    float VB = (VB_ADC * 3.29 * 2) / 4096 + 0.4;
    Serial.println(VB_ADC);
    Serial.print(VB);
    Serial.println("V\n");

    VB *= 100;
    unsigned char VB_D0 = (unsigned int)VB / 100 % 10;
    unsigned char VB_D1 = (unsigned int)VB / 10 % 10;
    unsigned char VB_D2 = (unsigned int)VB % 10;
    
    Display_Num(VB_D0 * 10 + VB_D1,0,1,1,0);
    vTaskDelay(300);
    Display_Num(VB_D0 * 10 + VB_D2,0,1,0,1);
    vTaskDelay(300);
    Display_Num(VB_D0 * 10 + VB_D1,0,1,1,0);
    vTaskDelay(300);
    Display_Num(VB_D0 * 10 + VB_D2,0,1,0,1);
    vTaskDelay(300);
}

void Nixie_Fresh(void)
{
    Display_Num(9,0,0,0,0);vTaskDelay(Fresh_Delay_Time);
    Display_Num(98,1,0,0,0);vTaskDelay(Fresh_Delay_Time);
    Display_Num(87,1,1,0,0);vTaskDelay(Fresh_Delay_Time);
    Display_Num(76,1,1,1,0);vTaskDelay(Fresh_Delay_Time);
    Display_Num(65,1,1,1,1);vTaskDelay(Fresh_Delay_Time);
    Display_Num(54,1,1,1,1);vTaskDelay(Fresh_Delay_Time);
    Display_Num(43,1,1,1,1);vTaskDelay(Fresh_Delay_Time);
    Display_Num(32,0,1,1,1);vTaskDelay(Fresh_Delay_Time);
    Display_Num(21,0,0,1,1);vTaskDelay(Fresh_Delay_Time);
    Display_Num(10,0,0,0,1);vTaskDelay(Fresh_Delay_Time);
    Display_Num(0,0,0,0,0);vTaskDelay(Fresh_Delay_Time);
}
